//
//  DXData.m
//  suremodv
//
//  Created by dd on 13-9-19.
//  Copyright (c) 2013年 xiaomuzhi. All rights reserved.
//

#import "DXData.h"
#import "DXUtil.h"

@implementation DXData

+ (id)sharedInstance
{
    static DXData *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        [sharedInstance restore];
    });
    
    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        
    }
    
    return self;
}

- (NSNumber *)userId
{
    NSNumber *userId = self.user[@"id"];
    return userId;
}

- (void)restore
{
    [DX i:@"restore data called"];
    
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    /*
    self.token = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:@"token"]];
    self.user = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:@"user"]];
    self.param = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:@"param"]];
    self.data = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByAppendingPathComponent:@"data"]];
    */

    self.token = [NSString stringWithContentsOfFile:[path stringByAppendingPathComponent:@"token"] encoding:NSUTF8StringEncoding error:nil];

    NSString *userJsonString = [NSString stringWithContentsOfFile:[path stringByAppendingPathComponent:@"user"] encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *user = @{};
    if (userJsonString.length > 0)
    {
        user = [DX jsonDecode:userJsonString];
    }
    self.user = [user mutableCopy];
    
    NSString *paramJsonString = [NSString stringWithContentsOfFile:[path stringByAppendingPathComponent:@"param"] encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *param = @{};
    if (paramJsonString.length > 0)
    {
        param = [DX jsonDecode:paramJsonString];
    }
    self.param = [param mutableCopy];
    
    NSString *dataJsonString = [NSString stringWithContentsOfFile:[path stringByAppendingPathComponent:@"data"] encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *data = @{};
    if (dataJsonString.length > 0)
    {
        data = [DX jsonDecode:dataJsonString];
    }
    self.data = [data mutableCopy];
    
//    if (self.user == nil) self.user = [[NSMutableDictionary alloc] init];
//    if (self.param == nil) self.param = [[NSMutableDictionary alloc] init];
//    if (self.data == nil) self.data = [[NSMutableDictionary alloc] init];
}

- (void)save
{
    [DX i:@"save data called"];
    
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    /*
    [NSKeyedArchiver archiveRootObject:self.token toFile:[path stringByAppendingPathComponent:@"token"]];
    [NSKeyedArchiver archiveRootObject:self.user toFile:[path stringByAppendingPathComponent:@"user"]];
    [NSKeyedArchiver archiveRootObject:self.param toFile:[path stringByAppendingPathComponent:@"param"]];
    [NSKeyedArchiver archiveRootObject:self.data toFile:[path stringByAppendingPathComponent:@"data"]];
    */
     
    NSError *error;
    
    [self.token writeToFile:[path stringByAppendingPathComponent:@"token"] atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
    [[DX jsonEncode:self.user] writeToFile:[path stringByAppendingPathComponent:@"user"] atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
    [[DX jsonEncode:self.param] writeToFile:[path stringByAppendingPathComponent:@"param"] atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
    [[DX jsonEncode:self.data] writeToFile:[path stringByAppendingPathComponent:@"data"] atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
}

- (void)empty
{
    self.user = [[NSMutableDictionary alloc] init];
    self.token = nil;
    self.param = [[NSMutableDictionary alloc] init];
    self.data = [[NSMutableDictionary alloc] init];
    [self save];
}

- (void)setToken:(NSString *)token
{
    _token = token;
}

- (NSString *)stringParamForKey:(NSString *)key
{
    if (self.param[key])
    {
        return self.param[key];
    }
    
    return @"";
}

- (NSDictionary *)userDefaultFromAddress
{
    if (self.user && ![DX isNull:self.user[@"address_list"]])
    {
        NSArray *addressList = self.user[@"address_list"];
        for (NSDictionary *address in addressList)
        {
            NSNumber *priority = address[@"priority"];
            if (priority.integerValue > 0)
            {
                return address;
            }
        }
    }
    
    return nil;
}

@end
